//@作者：SuoNi
//@邮箱：suoni1919@gmail.com

#include<stdio.h>
#include<stdlib.h>
#include<memory.h>
#include<string.h>

int token; //当前的token
char *src, *old_src; //源码字符串指针
int poolsize; //默认 （文本/数据/栈） 大小
int line;

//编译原理区域：
int *text,     //文本区text segment
    *old_text,  //for dump text segment
    *stack;     //调用帧 - 栈区 stack
char *data; //数据段 data segment

//模拟四个虚拟寄存器
int *pc,*bp,*sp,ax,cycle; // virtual machine registers


//CUP指令集模拟
// instructions
//带参指令集在前 无参在后
enum { LEA ,IMM ,JMP ,CALL,JZ  ,JNZ ,ENT ,ADJ ,LEV ,LI  ,LC  ,SI  ,SC  ,PUSH,
       OR  ,XOR ,AND ,EQ  ,NE  ,LT  ,GT  ,LE  ,GE  ,SHL ,SHR ,ADD ,SUB ,MUL ,DIV ,MOD ,
       OPEN,READ,CLOS,PRTF,MALC,MSET,MCMP,EXIT };


void next(){
    token = *src++;
    return ;
}

void expression(int level){
    //do things

}

void program(){
    next();
    while (token>0)
    {
        printf("token is:%c\n",token);
        next();
    }
    
}


int eval(){
    return 0;
}

int main(int argc,char **argv){
    int i,fd;

    argc--;
    argv++;
    poolsize = 256*1024; //任意大小字节
    line = 1;

    if(!(text = malloc(poolsize))){
        printf("could not mallo(%d) for data area \n",poolsize);
        return -1;
    }
     if (!(data = malloc(poolsize))) {
        printf("could not malloc(%d) for data area\n", poolsize);
        return -1;
    }
    if (!(stack = malloc(poolsize))) {
        printf("could not malloc(%d) for stack area\n", poolsize);
        return -1;
    }
    if (!(symbols = malloc(poolsize))) {
        printf("could not malloc(%d) for symbol table\n", poolsize);
        return -1;
    }

    memset(text, 0, poolsize);
    memset(data, 0, poolsize);
    memset(stack, 0, poolsize);
    memset(symbols, 0, poolsize);

    bp = sp = (int*)((int)stack+poolsize);
    ax = 0;

    if((fd = open(*argv,0))<0){
        printf("could not open(%s)\n",*argv);
        return -1;
    }

    if(src = old_src = malloc(poolsize)){
        printf("could not malloc (%d) for source area\n",poolsize);
        return -1;
    }

    //read thr source file

    if((i == read(fd,src,poolsize-1))<=0){
        printf("read() returned %d\n",i);
        return -1;
    }

    //标示文件结尾处 end of file
    src[i]=0;
    close(fd);

    program();
    return eval();
}